﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chapter09.View
{
    using Chapter09.ViewModel;

    /// <summary>
    /// SimpleDocumentWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MonitorWindow : Window
    {
        private bool isDirty = false;
        private int texBoxTrigerNum = 0;

        public MonitorWindow()
        {
            InitializeComponent();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            isDirty = true;
        }

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == DataCommands.Undo)
            {
                if (listBox1 == null || listBox1.Items.Count == 0)
                    e.CanExecute = false;
                else
                    e.CanExecute = true;
            }
            if (e.Command == ApplicationCommands.Save)
            {
                e.CanExecute = isDirty;
            }
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Source is ICommandSource)
                return;
            if (e.Command == DataCommands.Undo && texBoxTrigerNum < 2)
            {
                texBoxTrigerNum++;
                return;
            }
            var txt = e.Source as TextBox;
            if (txt != null)
            {
                var cmd = (RoutedUICommand)e.Command;
                var historyItem = new CommandHistoryItem(cmd.Name, txt, "Text", txt.Text);
                var item = new ListBoxItem();
                item.Content = historyItem;
                listBox1.Items.Add(historyItem);
            }
            if (e.Command == ApplicationCommands.Save)
            {
                MessageBox.Show(this, ((TextBox)e.Source).Text, "已保存");
                isDirty = false;
            }
            if (e.Command == DataCommands.Undo)
            {
                var historyItem = (CommandHistoryItem)listBox1.Items[listBox1.Items.Count - 1];
                if (historyItem.CanUndo)
                {
                    historyItem.Undo();
                }
                listBox1.Items.Remove(historyItem);
                texBoxTrigerNum = 0;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            AddHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(CommandBinding_Executed));
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            RemoveHandler(CommandManager.PreviewExecutedEvent, new ExecutedRoutedEventHandler(CommandBinding_Executed));
        }
    }
}
